package com.musichord.core.event;

import java.util.List;
import java.util.Vector;

import javax.sound.midi.MidiEvent;

import com.musichord.core.message.NoteOffMessage;
import com.musichord.core.message.NoteOnMessage;
import com.musichord.core.unit.note.Note;

public class NoteEvent implements MCEvent {

	private long tick = 0;
	private Note note;

	public NoteEvent(Note note) {
		this.note = note;
	}

	public NoteEvent(Note note, long tick) {
		this.note = note;
		this.tick = tick;
	}

	public NoteEvent(int pitch, int velocity, int duration) {
		this(new Note(pitch, velocity, duration));
	}

	public NoteEvent(int pitch, int velocity, int duration, long tick) {
		this(new Note(pitch, velocity, duration), tick);
	}

	public List<MidiEvent> getMidiEvents() {
		Vector<MidiEvent> resultList = new Vector<MidiEvent>();

		NoteOnMessage noteOnMessage = new NoteOnMessage(note.getPitch(),
				note.getVelocity());
		NoteOffMessage noteOffMessage = new NoteOffMessage(note.getPitch());

		resultList.add(new MidiEvent(noteOnMessage, tick));
		resultList
				.add(new MidiEvent(noteOffMessage, tick + note.getDuration()));

		return resultList;
	}

	@Override
	public int getDuration() {
		return this.note.getDuration();
	}
}
